<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>添加企业</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="rules">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" size="mini" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" size="mini" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" size="mini" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode" width="100%">
              <el-select v-model="addForm.industryCode" placeholder="请选择所在行业" size="mini">
                <el-option v-for="item in industryList" :key="item.industryCode" :label="item.industryName" :value="item.industryCode" />
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" size="mini" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" size="mini" />
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseId">
              <el-upload ref="clearFile" action="#" :before-upload="beforeUpload" :http-request="uploadRequest">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">支持扩展名: .png .jpg .jpeg,文件大小不得超过5M</div>
                <!-- 添加以下部分用于显示上传后的图片 -->
                <img v-if="addForm.businessLicenseUrl" :src="addForm.businessLicenseUrl" style="max-width: 100%; margin-top: 10px;">
              </el-upload>
            </el-form-item>

          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getIndustryListAPI, uploadAPI, addEnterpriseAPI, getEnterpriseAPI } from '@/api/enterprise'
export default {
  data() {
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      rules: {
        name: [
          { required: true, message: '企业名称为必填', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '法人为必填', trigger: 'blur' }
        ],
        registeredAddress: [
          { required: true, message: '注册地址为必填', trigger: 'blur' }
        ],
        industryCode: [
          { required: true, message: '所在行业为必填', trigger: 'change' }
        ],
        contact: [
          { required: true, message: '企业联系人为必填', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '企业联系人电话为必填', trigger: 'blur' },
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if (!/^1[3456789]\d{9}$/.test(value)) {
                callback(new Error('请输入正确的手机号'))
              } else {
                callback()
              }
            }
          }
        ],
        businessLicenseId: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ]

      },
      //   行业列表信息
      industryList: [],
      id: this.$route.params.id
    }
  },
  created() {
    if (this.id) {
      this.getEnterpriseDetailAPI()
    }
  },
  mounted() {
    this.getIndustryListAPI()
  },
  methods: {
    // 获取行业列表信息
    async getIndustryListAPI() {
      const res = await getIndustryListAPI()
      //   console.log(res)
      this.industryList = res.data
      // console.log(this.industryList)
    },
    // 文件上传之前的检查工作 文件类型 & 文件大小
    beforeUpload(file) {
      const isJPG = ['image/jpeg', 'image/jpg', 'image/png'].includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG JPEG PNG格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isJPG && isLt2M
    },
    // 上传之后
    async uploadRequest(param) {
      // console.log(param)
      const file = param.file
      // 处理formData类型参数
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'businessLicense')
      const res = await uploadAPI(formData)
      // console.log(res)
      this.addForm.businessLicenseUrl = res.data.url
      this.addForm.businessLicenseId = res.data.id

      // 单独校验表单，清除错误信息
      this.$refs.ruleForm.validateField('businessLicenseId')
    },
    // 重置
    reset() {
      this.$refs.ruleForm.resetFields()
      // 清除上传文件列表
      this.$refs.clearFile.clearFiles()
      this.addForm = { }
    },
    // 确定
    async confirm() {
      // 1. 表单校验
      await this.$refs.ruleForm.validate()
      // 2. 调用接口
      await addEnterpriseAPI(this.addForm)
      // 3. 提示信息
      this.$message.success('添加企业成功')
      // 4. 重置表单
      this.reset()
      // 5. 返回列表页
      this.$router.push('/park/enterprise')
    },
    // 获取企业详情
    async getEnterpriseDetailAPI() {
      const res = await getEnterpriseAPI(this.id)
      console.log(res)
      const { name, legalPerson, registeredAddress, industryCode, contact, contactNumber, businessLicenseUrl, businessLicenseId } = res.data
      this.addForm = { name, legalPerson, registeredAddress, industryCode, contact, contactNumber, businessLicenseUrl, businessLicenseId }
    }
  }
}
</script>

  <style scoped lang="scss">
  .add-enterprise {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      justify-content: space-between;
      padding: 0 20px;
      height: 64px;
      line-height: 64px;
      font-size: 16px;
      font-weight: bold;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;
        .title {
          height: 60px;
          line-height: 60px;
          padding-left:20px;
        }
        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;
          .el-form{
            display: flex;
            flex-wrap: wrap;
            .el-form-item{
              width: 50%;
            }
          }
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
    }
  }
  </style>
